package com.example.userservice.novel.entity;


import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Entity
@Table(name = "novel_structure")
@EntityListeners(AuditingEntityListener.class)
public class NovelStructureEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private String novelName;

    @Column(length = 1000)
    private String theme;

    @Column(columnDefinition = "TEXT")
    private String plotOutline;

    @Column(columnDefinition = "TEXT")
    private String worldSetting;

    @Column(length = 500)
    private String writingStyle;

    @Column(columnDefinition = "TEXT")
    private String rawResponse;

    private String genre;
    private String status = "ACTIVE"; // ACTIVE, DELETED

    @CreatedDate
    private Date createdAt;

    @LastModifiedDate
    private Date updatedAt;

    private Date generatedAt;

    // 关联的角色列表
    @OneToMany(mappedBy = "novelStructure", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<CharacterEntity> characters = new ArrayList<>();

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getNovelName() {
        return novelName;
    }

    public void setNovelName(String novelName) {
        this.novelName = novelName;
    }

    public String getTheme() {
        return theme;
    }

    public void setTheme(String theme) {
        this.theme = theme;
    }

    public String getPlotOutline() {
        return plotOutline;
    }

    public void setPlotOutline(String plotOutLine) {
        this.plotOutline = plotOutLine;
    }

    public String getWorldSetting() {
        return worldSetting;
    }

    public void setWorldSetting(String worldSetting) {
        this.worldSetting = worldSetting;
    }

    public String getWritingStyle() {
        return writingStyle;
    }

    public void setWritingStyle(String writingStyle) {
        this.writingStyle = writingStyle;
    }

    public String getRawResponse() {
        return rawResponse;
    }

    public void setRawResponse(String rawResponse) {
        this.rawResponse = rawResponse;
    }

    public String getGenre() {
        return genre;
    }

    public void setGenre(String genre) {
        this.genre = genre;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Date getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }

    public Date getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(Date updatedAt) {
        this.updatedAt = updatedAt;
    }

    public Date getGeneratedAt() {
        return generatedAt;
    }

    public void setGeneratedAt(Date generatedAt) {
        this.generatedAt = generatedAt;
    }

    public List<CharacterEntity> getCharacters() {
        return characters;
    }

    public void setCharacters(List<CharacterEntity> characters) {
        this.characters = characters;
    }

    // 转换到DTO
    public NovelStructure toDTO() {
        NovelStructure dto = new NovelStructure();
        dto.setId(this.id);
        dto.setNovelName(this.novelName);
        dto.setTheme(this.theme);
        dto.setPlotOutline(this.plotOutline);
        dto.setWorldSetting(this.worldSetting);
        dto.setWritingStyle(this.writingStyle);
        dto.setRawResponse(this.rawResponse);
        dto.setGeneratedAt(this.generatedAt);
//        dto.setCreatedAt(this.createdAt);

        // 转换角色
        if (this.characters != null) {
            List<SimpleCharacter> characterDTOs = this.characters.stream()
                    .map(CharacterEntity::toDTO)
                    .collect(Collectors.toList());
            dto.setMainCharacters(characterDTOs);
        }

        return dto;
    }

    // 从DTO更新
    public void updateFromDTO(NovelStructure dto) {
        this.novelName = dto.getNovelName();
        this.theme = dto.getTheme();
        this.plotOutline = dto.getPlotOutline();
        this.worldSetting = dto.getWorldSetting();
        this.writingStyle = dto.getWritingStyle();
        this.rawResponse = dto.getRawResponse();
        this.generatedAt = dto.getGeneratedAt();
    }
}
